﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Hedgehog
{
    internal class SerpinskiTriangle : IFigure
    {
        private int _deepness;
        private int _offsetX;
        public int offsetX
        {
            get { return _offsetX; }
            set
            {
                _offsetX = value;

                if (s1 != null)
                    s1.offsetX = value;

                if (s2 != null)
                    s2.offsetX = value;

                if (s3 != null)
                    s3.offsetX = value;
            }
        }

        private int _offsetY;
        public int offsetY
        {
            get { return _offsetY; }
            set
            {
                _offsetY = value;

                if (s1 != null)
                    s1.offsetY = value;

                if (s2 != null)
                    s2.offsetY = value;

                if (s3 != null)
                    s3.offsetY = value;
            }
        }

        private Point _a;
        private Point _b;
        private Point _c;

        public Point A
        {
            get { return new Point(_a.X + offsetX, _a.Y + offsetY); }
        }

        public Point B
        {
            get { return new Point(_b.X + offsetX, _b.Y + offsetY); }
        }

        public Point C
        {
            get { return new Point(_c.X + offsetX, _c.Y + offsetY); }
        }

        protected Point AB
        {
            get { return new Point((A.X + B.X)/2, (A.Y + B.Y)/2); }
        }

        protected Point BC
        {
            get { return new Point((B.X + C.X)/2, (B.Y + C.Y)/2); }
        }

        protected Point CA
        {
            get { return new Point((C.X + A.X)/2, (C.Y + A.Y)/2); }
        }

        private SerpinskiTriangle s1; 
        private SerpinskiTriangle s2; 
        private SerpinskiTriangle s3; 

        public SerpinskiTriangle(Point a, Point b, Point c, int deepness)
        {
            _deepness = deepness;
            _a = a;
            _b = b;
            _c = c;
            if (deepness == 0) return;
            s1 =new SerpinskiTriangle(A, AB, CA, deepness-1);
            s2 =new SerpinskiTriangle(CA, BC, C, deepness-1);
            s3 = new SerpinskiTriangle(AB, B, BC, deepness-1);
        }

        public void Draw(Graphics g)
        {
            g.DrawLine(Pens.Black, A, B);
            g.DrawLine(Pens.Black, B, C);
            g.DrawLine(Pens.Black, A, C);

            if (s1!=null)
                s1.Draw(g);
            if (s2 != null)
                s2.Draw(g);
            if (s3 != null)
                s3.Draw(g);
        }



        public bool ChekIfPointInside(int x, int y)
        {
            return TriangleHelper.CheckIfPointInsideTriangle(A, B, C, new Point(x, y));

        }

        public object Clone()
        {
             return new SerpinskiTriangle(A,B,C, _deepness);
        }
    }
}
